package com.next.servlet;

import com.alibaba.fastjson.JSON;
import com.bean.Announce;
import com.bean.AnnouncePage;
import com.dao.AnnounceDao;
import com.utils.DateUtils;
import com.utils.PageUtil;
import com.utils.ResponseUtils;
import com.utils.StringUtils;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AnnounceServlet
 * @Desc 后台公告控制层
 * @Author xiaoding
 * @Date 2021/1/17 20:54
 * @Version 1.0
 */
@WebServlet(name = "NextAnnounceServlet",value = "/NextAnnounceServlet")
public class AnnounceServlet extends HttpServlet {
    private AnnounceDao announceDao = new AnnounceDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数，判断功能模块
        String str = request.getParameter("flag");

        //判断功能模块
        if (str.equals("select")) {
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //查询所有公告的数量
            int count = announceDao.announceCount();
            //定义访问的Servlet
            String targetUrl = "NextAnnounceServlet";
            //定义参数
            String param = "flag=select";
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用获取数据的方法
            List<Announce> announce = announceDao.announceSelect(new AnnouncePage(page,PageUtil.pageSize));
            //设置会话
            request.setAttribute("announce",announce);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("next/announce.jsp").forward(request, response);
        }else if (str.equals("search")){
            //获取公告标题
            String titles = request.getParameter("titles");

            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //根据公告标题，查询所有公告的数量
            int count = announceDao.announceSearchCount("%" + titles + "%");
            //定义访问的Servlet
            String targetUrl = "NextAnnounceServlet";
            //定义参数
            String param = "flag=search&page="+page+"&titles="+titles;
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用获取数据的方法
            List<Announce> announce = announceDao.announceSearch(new AnnouncePage("%" + titles + "%",page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("announce",announce);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("next/announce.jsp").forward(request, response);
        }else if (str.equals("selectId")) {
            //获取公告Id
            int id = Integer.parseInt(request.getParameter("id"));

            //调用查询的方法
            Announce announce = announceDao.announceSelectId(id);

            //设置会话
            request.setAttribute("announce",announce);

            //分页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //设置搜索名称
            String titles = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("titles"))) {
                titles = request.getParameter("titles");
            }

            //跳转页面
            request.getRequestDispatcher("next/updateAnnounce.jsp?page="+page+"&titles="+titles+"&id="+id).forward(request,response);
        }else if (str.equals("add")) {
            //获取参数
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            int uId = Integer.parseInt(request.getParameter("uId"));
            //发布时间、修改时间
            Date date1 = new Date();
            Date date2 = new Date();
            //设置路径、状态
            String message = null;
            String url = null;

            //调用添加方法
            int a = announceDao.addAnnounce(new Announce(title,content,uId,date1,date2));
            //判断是否添加成功
            if (a > 0) {
                message = "添加成功";
                url = "NextAnnounceServlet?flag=select";
            }else {
                message = "添加失败";
                url = "NextAnnounceServlet?flag=select";
            }

            //跳转页面
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("update")) {
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //设置路径、状态
            String message = null;
            String url = null;

            //获取参数
            int id = Integer.parseInt(request.getParameter("id"));
            System.out.println(id);
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            int uId = Integer.parseInt(request.getParameter("uId"));
            System.out.println(DateUtils.parseDate(request.getParameter("publishingDate")));
            Date date = DateUtils.parseDate(request.getParameter("publishingDate"));
            System.out.println(date);
            //修改时间
            Date date1 = new Date();

            //调用修改的方法
            int a = announceDao.updateAnnounce(new Announce(id,title,content,uId,date,date1));
            //判断是否修改成功
            if (a > 0) {
                message = "修改成功";
            }else {
                message = "修改失败";
            }

            //设置搜索名称
            String titles = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("titles"))) {
                titles = request.getParameter("titles");
                url = "NextAnnounceServlet?flag=search&page="+page+"&titles="+titles;
            }else {
                url = "NextAnnounceServlet?flag=select&page="+page;
            }

            //跳转页面
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("delete")) {
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //设置路径、状态
            String message = null;
            String url = null;

            //获取参数
            int id = Integer.parseInt(request.getParameter("id"));
            //调用删除的方法
            int a = announceDao.deleteAnnounce(id);
            //判断是否删除成功
            if (a > 0) {
                message = "删除成功";
            }else {
                message = "删除失败";
            }

            //设置搜索名称
            String titles = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("titles"))) {
                titles = request.getParameter("title");
                url = "NextAnnounceServlet?flag=search&page="+page+"&titles="+titles;
            }else {
                url = "NextAnnounceServlet?flag=select&page="+page;
            }

            //跳转页面
            ResponseUtils.writeMessage(response,message,url);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
